﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Week1.world.behavior
{
    /// <summary>
    /// base class that implement some basic methods such as getbehaviorhandlerid, settrigger, etc
    /// </summary>
    public class BehaviorBase: ABehavior
    {
        //TODO: implement rotate object i.e, createrotations x,y or z.

        // Max references
        public const int MAXREFERENCES = 200;
        /// <summary>
        /// Handler ID
        /// </summary>
        protected int iHandlerIDIndex;
        protected TriggerKey[] oTriggers = new TriggerKey[MAXREFERENCES];

        // contains indices for dead triggers.
        protected int[] iDeadIndices = new int[MAXREFERENCES];

        // When object dies, its index is stored in iDeadIndices. nextDeadIndedNo is the index to the iDeadIndices. 
        // nextDeadIndexNo variable makes it possible to keep track of where to put next dead index
        // nextDeadIndexNo and nextAvailableIndex forms a loop that keeps array with dead index with used indexes.
        // [-1,-1,3,5,-1]. -1 means slot is available. at this point nextDeadIndexNo = 4, and nextAvailabelIndex = 0
        // [2,-1,3,5,-1]. nextDeadIndexNo = 4, but nextAvailableIndex = 1 as slot at 0  was previously occupied.  
        protected int iNextDeadIndexNo = 0;
        protected int inextAvailableIndex = 0;

        protected BehaviorBase()
            : base()
        {
            
        }

        public override int getBehaviorHandlerID(DrawableGlobalVar oVars)
        {
            return iHandlerIDIndex++;
        }

        public override void execute(int handlerid, DrawableGlobalVar oVars, GameTime gameTime)
        {
            throw new NotImplementedException();
        }

        public override void initialize(int handlerid, DrawableGlobalVar oVars)
        {
            throw new NotImplementedException();
        }

        public override void setTrigger(int handler, TriggerKey trigger)
        {
            oTriggers[handler] = trigger;
        }

        public override void setBehaviorProperty(int handler, int instanceIndex, string name, float value)
        {
            throw new NotImplementedException();
        }

        public override void setBehaviorProperty(int handler, int instanceIndex, string name, int value)
        {
            throw new NotImplementedException();
        }

        public override void setBehaviorProperty(int handler, int instanceIndex, string name, ref Microsoft.Xna.Framework.Vector3 value)
        {
            throw new NotImplementedException();
        }

        public override void setBehaviorProperty(int handler, int instanceIndex, string name, string value)
        {
            throw new NotImplementedException();
        }

        public override void setBehaviorProperty(int handler, int instanceIndex, string name, DrawableGlobalVar value)
        {
            throw new NotImplementedException();
        }

        public override void setBehaviorProperty(int handler, int instanceIndex, string name, Microsoft.Xna.Framework.Vector3[] value)
        {
            throw new NotImplementedException();
        }

        public override void setBehaviorProperty(int handler, int instanceIndex, string name, long[] value)
        {
            throw new NotImplementedException();
        }

        public override void setBehaviorProperty(int handler, int instanceIndex, string name, Vector2[] value)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Check if trigger is active. 
        /// </summary>
        /// <param name="handlerid"></param>
        /// <returns></returns>
        protected bool isTriggerActive(int handlerid)
        {
            bool active = oTriggers[handlerid].oTriggetType == TriggerKey.TriggetType.GameUpdate;
            if (!active)
            {
                switch (oTriggers[handlerid].oTriggetType)
                {
                    case TriggerKey.TriggetType.Keyboard:
                        if (TriggerManager.getInstance().oKeyState.Length > 0)
                            for (int idx = 0; idx < TriggerManager.getInstance().oKeyState.Length && !active; idx++)
                            {
                                active = oTriggers[handlerid].Key == TriggerManager.getInstance().oKeyState[idx];
                            }
                        break;
                    
                    case TriggerKey.TriggetType.GameTimeSec:
                        TimeSpan oSpan = TriggerManager.getInstance().oGameTime.TotalGameTime;
                        active = oSpan.Seconds > oTriggers[handlerid].lSecStartTime && oSpan.Seconds < oTriggers[handlerid].lSecEndTime ? true : false;
                        break;

                }
            }

            return active;
        }
    }
}
